<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of elggForm
 *
 * @author german
 */
require_once 'elggForm_validators.php';

class elggForm {

    public $object = false;
    public $form_body = '';
    public $html_form = '';
    public $form_header = array();
    public $form_validator = array();
    public $form_elements = array();
    public $errors = array();
    public $plugin_name = '';

    //--- Magic methods over here
    public function __construct() {
       $context = get_context();
       if ($context) {
           $this->plugin_name = $context;
       }
    }

    public function __toString() {
        return $this->getForm();
    }

    //-- End magic methods
    //-- Private Functions --
    private function _setFormBody() {
        $this->form_body = "";

        foreach ($this->forms_elements as $form_input ) {
            $input_name = $form_input['attributes']['internalname'];

            if (isset($input_name)) {
                $input = $this->getInput($input_name);
                if ($input) {
                    $this->form_body .= $input;
                }
            } else {
                $this->errors['protected:info'][] = elgg_echo('elggForm:protected:setInput:notFormBody');
                return false;
            }
        }
            return $this->form_body;
    }

    private function setPluginName($plugin_name) {
        $this->plugin_name = $plugin_name;
    }

    //-- End Private Functions
    // Set the input with them validators and attributes
    public function setInput($type, $validator = array(), $attributes = array()) {
        if ($this->plugin_name == '') {
            $this->setPluginName(get_context());
        }
        
        if (empty($validator['name'])) {
            $this->errors['protected:info'][] = elgg_echo('elggForm:protected:setInput:name_not_set');
            return false;
        }

        $input_name = $validator['name'];
        unset($validator['name']);

        foreach ($validator as $key => $value) {
            $this->form_validator[$input_name] = array($key, $value);
        }

        if ($this->object) {
            $attributes = array_merge($attributes, array('internalname' => $input_name, 'value' => $this->object->$input_name));
        } else {
            $attributes = array_merge($attributes, array('internalname' => $input_name, 'value' => ''));
        }

        $myHtml = '';
        $myLabel = '<label class="elggForm-helper">'.elgg_echo('elggForm:label:'.$this->plugin_name.':'.$input_name).'</label>';

        if (isset($attributes['has_label'])) {
            if ($attributes['has_label'] == false) {
                $myLabel = '';
            }
            unset($attributes['has_label']);
        }
        
        $hasHelper = false;
        if(isset($attributes['has_helper'])) {
            if ($attributes['has_helper'] == true) {
                $hasHelper = true;
            }
            unset($attributes['has_helper']);
        }

        $myHtml .= $myLabel;
        $myHtml .= elgg_view('input/'.$type, $attributes);

        if ($hasHelper) {
            $myHtml .= '<span class="elggForm-helper">'.elgg_echo('elggForm:helper:'.$this->plugin_name.':'.$input_name).'</span>';
        }

        $myFormsElements = array(
            'html' => $myHtml,
            'attributes' => $attributes,
        );

        $this->forms_elements[$input_name] = $myFormsElements;
    }

    public function getInput($input_name) {
        if (isset($this->forms_elements[$input_name]['html'])) {
            return $this->forms_elements[$input_name]['html'];
        } else {
            $this->errors['protected:info'][] = elgg_echo('elggForm:protected:setInput:notGetInput');
            return false;
        }
    }

    // Set the form header and the action of the form for elgg
    public function formHeader($attributes = array()) {

        $guid = get_input('guid', false);
        $action = 'add';

        if ($guid) {
            $action = 'edit?guid='.$guid;
        }
        
        $defaults = array(
            'action' => $action,
            'plugin_name' => $this->plugin_name,
            'internalname' => 'elggForm_' . time(),
        );

        $this->form_header = array_merge($defaults, $attributes);
        global $CONFIG;
        $this->form_header['action'] = $CONFIG->wwwroot . 'action/' . $this->form_header['plugin_name'] . '/' . $this->form_header['action'];

        return true;
    }

    //Get the whole form
    public function getForm($with_submit = true) {
        if (empty($this->form_header)) {
            $this->formHeader();
        }
        if ($this->_setFormBody()) {
            if ($with_submit) {
                $this->form_body .= elgg_view('input/submit', array('value' => elgg_echo('submit')));
            }
            return elgg_view('input/form', array_merge($this->form_header, array('body' => $this->form_body)));
        } else {
            return false;
        }
    }

    public function getFormBody() {
        return $this->_setFormBody();
    }

}